<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="800px" 
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="Model" :rules="dialogProps.action === 'add' ? addRules : editRules" 
      ref="from" label-width="80px" label-position="right" size="small">
      <el-tabs type="border-card" v-model="activeTabName">
        <el-tab-pane name="tab1" label="基本休息" class="user-info-tab-pane">
          <el-row>
            <el-col :span="12">
              <el-form-item label="档案编号" prop="fileNumber">
                <el-input :maxlength="32" v-model="Model.fileNumber" autofocus></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证" prop="idCard">
                <el-input :maxlength="32" v-model="Model.idCard"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="登录密码" prop="password">
                <el-input :maxlength="32" v-model="Model.password" autofocus></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="手机号" prop="phoneNumber">
                <el-input :maxlength="32" v-model="Model.phoneNumber"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input :maxlength="32" v-model="Model.name" ref="focus"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="sex">
                <el-input :maxlength="32" v-model="Model.sex" autofocus></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="民族" prop="nation">
                <el-input :maxlength="32" v-model="Model.nation"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年龄" prop="age">
                <el-input :maxlength="32" v-model="Model.age" autofocus></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="出生日期" prop="birthday">
                <el-input :maxlength="32" v-model="Model.birthday"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input :maxlength="32" v-model="Model.email" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="婚姻状态" prop="marital">
                <el-input :maxlength="32" v-model="Model.marital"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职业" prop="profession">
                <el-input :maxlength="32" v-model="Model.profession" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="文化程度" prop="culture">
                <el-input :maxlength="32" v-model="Model.culture"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="籍贯" prop="">
                <el-input :maxlength="32" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="健康管理师" prop="healthManager">
                <el-input :maxlength="32" v-model="Model.healthManager"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="档案状态" prop="status">
                <el-select :maxlength="32" v-model="Model.status" >
                  <el-option label="启用" value="启用"></el-option>
                  <el-option label="禁用" value="禁用"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :maxlength="32" v-model="Model.remark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-form-item label="密码" prop="password" v-show="dialogProps.action === 'add'">
            <el-col :span="16">
              <el-input type="password" :maxlength="32" v-model="Model.password" auto-complete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPassword" v-show="dialogProps.action === 'add'">
            <el-col :span="16">
              <el-input type="password" :maxlength="32" v-model="Model.checkPassword" auto-complete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="禁用">
            <el-switch v-model="Model.locked"></el-switch>
          </el-form-item> -->
        </el-tab-pane>
        <el-tab-pane label="动态信息">

          <el-row>
            <el-col :span="6">
              <el-form-item label="身高" prop="stature">
                <el-input :maxlength="32" v-model="Model.stature"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="体重" prop="weight">
                <el-input :maxlength="32" v-model="Model.weight"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="舒张压" prop="diastolicPressure">
                <el-input :maxlength="32" v-model="Model.diastolicPressure"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="收缩压" prop="systolicPressure">
                <el-input :maxlength="32" v-model="Model.systolicPressure"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="呼吸" prop="respiratoryFrequency">
                <el-input :maxlength="32" v-model="Model.respiratoryFrequency"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="体温" prop="bodyTemperature">
                <el-input :maxlength="32" v-model="Model.bodyTemperature"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="腰围" prop="waistline">
                <el-input :maxlength="32" v-model="Model.waistline"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="臀围" prop="hipline">
                <el-input :maxlength="32" v-model="Model.hipline"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="BMI" prop="bmi">
                <el-input :maxlength="32" v-model="Model.bmi"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="血氧饱和度" prop="oxyhemoglobinSaturation">
                <el-input :maxlength="32" v-model="Model.oxyhemoglobinSaturation"></el-input>%
              </el-form-item>
            </el-col>
          </el-row>

        </el-tab-pane>
        <el-tab-pane label="病史">

          <el-row>
            <el-col :span="24">
              <el-form-item label="既往史" prop="previousHistory">
                <el-input type="textarea" :maxlength="32" v-model="Model.previousHistory"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="家族史" prop="familyHistory">
                <el-input type="textarea" :maxlength="32" v-model="Model.familyHistory"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="过敏史" prop="allergicHistory">
                <el-input type="textarea" :maxlength="32" v-model="Model.allergicHistory"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="病史" prop="medicalHistory">
                <el-input type="textarea" :maxlength="32" v-model="Model.medicalHistory"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="慢性病" prop="chronicDisease">
                <el-input type="textarea" :maxlength="32" v-model="Model.chronicDisease"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>     
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('from')">确 定</el-button>
    </span>    
  </el-dialog>
</template>


<script>
import { saveUser, updateUser } from '@/api/user'
import {updateEmp} from '@/api/emp'
import { addMember,updateMember } from '@/api/member'
export default {
  name: "user-edit",
  data() {
    // var validatePassword = (rule, value, callback) => {
    //   if (value === '' || value === undefined) {
    //     callback(new Error('请输入密码'));
    //   } else {
    //     if (this.Model.checkPassword !== '') {
    //       this.$refs.from.validateField('checkPassword');
    //     }
    //     callback()
    //   }
    // }
    // var validateCheckPassword = (rule, value, callback) => {
    //   if (value === '' || value === undefined) {
    //     callback(new Error('请再次输入密码'))
    //   } else if (value !== this.Model.password) {
    //     callback(new Error('两次输入密码不一致!'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loading: false,
      Model: {
        id:0,
        fileNumber:'',
        name:'',
        sex:'',
        idCard:'',
        phoneNumber:'',
        password:'',
        nation:'',
        age:'',
        birthday:'',
        email:'',
        marital:'',
        profession:'',
        culture:'',
        healthManager:'',
        status:'启用',
        remark:'',
        stature:'',
        weight:'',
        diastolicPressure:'',
        systolicPressure:'',
        respiratoryFrequency:'',
        bodyTemperature:'',
        waistline:'',
        hipline:'',
        bmi:'',
        oxyhemoglobinSaturation:'',
        previousHistory:'',
        familyHistory:'',
        allergicHistory:'',
        medicalHistory:'',
        chronicDisease:'',
        regTime:''
      },
      roleCheckboxList: {
        checkAll: false,
        isIndeterminate: true,
        roles: []
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      activeTabName: 'tab1',
      addRules: {
        name: [
          { required: true, message: '请输入会员名称', trigger: 'blur' },
          { max: 32, message: '长度在 32 个字符内', trigger: 'blur' }
        ],
        fileNumber: [
          { required: true, message: '请输入档案号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' }
        ]
      },
      editRules: {
        name: [
          { required: true, message: '请输入会员名称', trigger: 'blur' },
          { max: 32, message: '长度在 32 个字符内', trigger: 'blur' }
        ],
        fileNumber: [
          { required: true, message: '请输入档案号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveUser()
          } else {
            this.doUpdateUser()
          }
          
        } else {
          return false
        }
      })
      
    },
    doUpdateUser() {
      this.loading = true
      console.log(this.Model)
      updateMember(this.Model)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.clearFrom()
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)      
      })
      
    },
    doSaveUser() {
      this.loading = true
      console.log(this.Model)
      addMember(this.Model)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.clearFrom()
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error) 
      })
      
    },    
    clearFrom(){
      for(let key in this.Model){
        this.Model[key]  = ''
        if(key=='status'){
          this.Model[key]  = '启用'
        }
      }
    },
    handleDialogClose() {
      this.dialogProps.visible = false  
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['from'].clearValidate()
        this.$refs['focus'].focus()
      })
    },
    getAllRoleId() {
      let allRoleId = []
      for(let role of this.roleCheckboxList.roles) {
        allRoleId.push(role.id)
      }
      return allRoleId
    },
    handleCheckAllChange(val) {
      this.Model.roleIds = (val ? this.getAllRoleId() : [])
      this.roleCheckboxList.isIndeterminate = false
    },
    handleCheckedRolesChange(value) {
      let checkedCount = value.length
      this.roleCheckboxList.checkAll = checkedCount === this.roleCheckboxList.roles.length
      this.roleCheckboxList.isIndeterminate = checkedCount > 0 && checkedCount < this.roleCheckboxList.roles.length
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })      
    }
  },
  mounted: function() {
    this.$nextTick(() => {  
      this.$on('openEditUserDialog', function(model) {
        this.Model = model
        // this.roleCheckboxList.roles = roles        
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改会员'
        this.activeTabName = 'tab1'
        this.dialogProps.visible = true
      })
      this.$on('openAddUserDialog', function() {
        // this.Model = {
        //   roleIds: []
        // }
        // this.roleCheckboxList.roles = roles
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加会员'
        this.activeTabName = 'tab1'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style lang="scss" scoped>
  .user-info-tab-pane {
    margin-top: 10px;
  }
</style>





